package com.example.cloudcity.servlet;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;
import java.util.ArrayList;

@WebServlet("/AddToCartServlet")
public class AddToCartServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        String gameId = request.getParameter("gameId");
        String gameName = request.getParameter("gameName");
        String gameMoney = request.getParameter("gameMoney");
        String gameImg = request.getParameter("gameImg");
        String gameTxt = request.getParameter("gameTxt");

        HttpSession session = request.getSession();
        ArrayList<String> cart = (ArrayList<String>) session.getAttribute("cart");
        if (cart == null) {
            cart = new ArrayList<>();
        }

        // 将商品信息添加到购物车
        String productInfo = gameId + "," + gameName + "," + gameMoney + "," + gameImg+","+gameTxt;
        cart.add(productInfo);
        session.setAttribute("cart", cart);

        response.sendRedirect("gouwuche.jsp"); // 重定向到购物车页面
    }
} 